package com.kl.component.spring.tool;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * spring上下文
 *
 * @author shigu_zjb
 *
 * <p>主要作用</p>
 * 1.手动为普通类注入bean
 *
 */
public class SpringApplicationContextTool implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringApplicationContextTool.applicationContext == null) {
            SpringApplicationContextTool.applicationContext = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
}
